// pages/editor/editor.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "",
    cursor: 0,
    realUserInfo: {},
    email: "输入邮箱",
    name: "输入姓名",
    phone: "输入手机",
    company: "输入公司",
    jop: "输入职位",
    bio: "输入介绍",
    gender: 0
  },
  previewImg:function(){
    var that = this
    wx.previewImage({
      current: that.data.realUserInfo.Avatar, // 当前显示图片的http链接
      urls: [that.data.realUserInfo.Avatar] // 需要预览的图片http链接列表
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'sex',
      success: function (res) {
        if (res.data != "") {
          that.setData({
            gender: res.data
          })
        }
      }
    })
  },
  blurName: function (e) {//姓名
    var name = e.detail.value
    this.setData({
      name: name
    })
  },
  blurPhone: function (e) {//手机
    var phone = e.detail.value
    this.setData({
      phone: phone
    })
  },
  blurEmail: function (e) {//邮箱
    var email = e.detail.value
    this.setData({
      email: email
    })
  },
  blurCompany: function (e) {//公司
    var company = e.detail.value
    this.setData({
      company: company
    })
  },
  blurJop: function (e) {//职位
    var jop = e.detail.value
    this.setData({
      jop: jop
    })
  },
  blurBio: function (e) {//简介
    var bio = e.detail.value
    this.setData({
      bio: bio
    })
  },
  //保存提交修改信息
  formSubmitUser: function (e) {
    var that = this
    var userInfor = e.detail.value
    var dto = {
      "Uid": app.globalData.userInfo.Uid,
      "NickName": (that.data.realUserInfo.NickName),
      "RealName": (userInfor.RealName),
      "Gender": (that.data.gender),
      "IdCard": "",
      "BDay": "",
      "RegionId": 0,
      "Address": (userInfor.address),
      "Bio": (userInfor.textarea),
      "Mobile": (userInfor.Mobile),
      "Email": (userInfor.Email),
      "Company": (userInfor.company),
      "JobTitle": (userInfor.jop)
    }
    wx.request({
      url: app.globalData.host + '/api/User/EditUserInfo',
      data: dto,
      success: function (res) {
        if (res.data.Code == 0) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          })
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            cancel: false
          })
          return
        }
      },
    })
  },
  /**
 * 获取会员用户信息
 */
  getUserInfo: function () {
    var that = this
    wx.request({
      url: app.globalData.host + '/api/User/GetUserInfo?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        console.log(res)
        if (res.data.Code == 0) {
          var realUserInfo = res.data.Data.UserInfo
          that.setData({
            realUserInfo: realUserInfo
          })
        }
      }
    })
  },
  //获取地理位置
  getMapLocaltion: function (e) {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          address: res.address
        })
      },
      fail: function () {
        wx.getSetting({
          success(res) {
            if (!res.authSetting['scope.userLocation']) {
              wx.authorize({
                scope: 'scope.userLocation',
                success(res) {
                  wx.chooseLocation({
                    success: function (res) {
                      that.setData({
                        address: res.address
                      })
                    },
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  //统计字数
  inputKey: function (e) {
    this.setData({
      cursor: e.detail.cursor
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})